import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import home from "@/views/home/index.vue";
import { useUserStore } from "@/stores/user";

NProgress.configure({
  showSpinner: false,
  easing: 'ease',
  speed: 500
});

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      meta: {
        title: "首页",
        keepAlive: true
      },
      component: home,
    },
    {
      path: "/login",
      name: "login",
      component: () => import(/* webpackChunkName: "login" */ "@/views/login/index.vue"),
      meta: {
        title: "登录"
      }
    },
    {
      path: "/tire-calculator",
      name: "tireCalculator",
      component: () => import(/* webpackChunkName: "tire-calculator" */ "@/views/tire-calculator/index.vue"),
      meta: {
        title: "轮胎周长计算器"
      }
    },
    {
      path: "/abs-calculator",
      name: "absCalculator",
      component: () => import(/* webpackChunkName: "abs-calculator" */ "@/views/abs-calculator/index.vue"),
      meta: {
        title: "ABS齿圈计算器"
      }
    },
    {
      path: "/iframes",
      name: "iframes",
      component: () => import(/* webpackChunkName: "sign-in" */ "@/views/iframes/index.vue"),
      meta: {
        title: "某核车型说明书"
      }
    },
    {
      path: "/controller-categories",
      name: "controllerCategories",
      component: () => import(/* webpackChunkName: "controller" */ "@/views/controller-categories/index.vue"),
      meta: {
        title: "控制器分类"
      }
    },
    {
      path: "/faq-contact",
      name: "faqContact",
      component: () => import(/* webpackChunkName: "faq" */ "@/views/faq-contact/index.vue"),
      meta: {
        title: "常见问题和联系我们"
      }
    },
    {
      path: "/clockIn",
      name: "clockIn",
      component: () => import(/* webpackChunkName: "faq" */ "@/views/clockIn/index.vue"),
      meta: {
        title: "打卡",
        showHeader: true
      }
    },
    {
      path: "/vehicle-info",
      name: "vehicleInfo",
      component: () => import(/* webpackChunkName: "vehicle" */ "@/views/vehicleInfo/index.vue"),
      meta: {
        title: "车辆信息",
        requiresAuth: true
      }
    },
    {
      path: "/mileage-detail",
      name: "mileageDetail",
      component: () => import(/* webpackChunkName: "vehicle" */ "@/views/mileage-detail/index.vue"),
      meta: {
        title: "骑行里程",
        requiresAuth: true
      }
    },
    {
      path: "/battery-detail",
      name: "batteryDetail",
      component: () => import(/* webpackChunkName: "vehicle" */ "@/views/battery-detail/index.vue"),
      meta: {
        title: "电池详情",
        requiresAuth: true
      }
    },
    {
      path: "/user-points",
      name: "userPoints",
      component: () => import(/* webpackChunkName: "user-points" */ "@/views/user-points/index.vue"),
      meta: {
        title: "我的积分",
        requiresAuth: true
      }
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

// 访问权限控制
router.beforeEach((to, from, next) => {
  window.test = (value) => {
    document.documentElement.style.setProperty(
      "--zoomin",
      value.fontSize + "px"
    );
  };
  NProgress.start();

  const userStore = useUserStore();
  const whiteList = ["/login", "/home", "/tire-calculator", "/abs-calculator", "/iframes", "/controller-categories", "/faq-contact"];
  
  // 检查页面是否需要登录权限
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 检查是否已登录
    if (!userStore.isLoggedIn) {
      // 未登录则跳转到登录页
      next({
        path: '/login',
        query: { redirect: to.fullPath } // 将要访问的页面路径作为参数传递给登录页
      });
    } else {
      // 已登录则正常进入
      next();
    }
  } else {
    // 不需要登录权限的页面直接进入
  next();
  }
});

router.afterEach((to) => {
  // 修改标题
  document.title = `慢牛电控-${to.meta.title || ""}`;
  NProgress.done();
});

export default router;
